﻿using System;
using System.Windows.Forms;
using NanoERP.Infrastructure;
using NanoERP.Model;
using NanoERP.Common;
using NanoERP.Business.BaseInfo;

namespace NanoERP.BaseInfo
{
    //[Module("FrmClientInfoEdit")]
    public partial class FrmClientInfoEdit : ModuleForm
    {
        Clientinfo _clientinfo;
        readonly Sysdictable _sysdictable;

        readonly UserinfoBLL _userinfoBLL;
        readonly DeptinfoBLL _deptinfoBLL;
        readonly ClientinfoBLL _clientinfoBll;
        readonly SysdictableBLL _sysdictablebLl;
        public FrmClientInfoEdit()
        {
            InitializeComponent();
        }

        public FrmClientInfoEdit(Sysdictable v)
        {
            InitializeComponent();

            _userinfoBLL = new UserinfoBLL();
            _deptinfoBLL = new DeptinfoBLL();
            _sysdictablebLl = new SysdictableBLL();
            _clientinfoBll = new ClientinfoBLL();
            _clientinfo = new Clientinfo();
            _sysdictable = v;

            FillFormInfo();
            BindFormContro();
        }

        public FrmClientInfoEdit(Clientinfo obj, Sysdictable v)
        {
            InitializeComponent();

            _userinfoBLL = new UserinfoBLL();
            _deptinfoBLL = new DeptinfoBLL();
            _sysdictablebLl = new SysdictableBLL();
            _clientinfoBll = new ClientinfoBLL();
            _clientinfo = obj;
            _sysdictable = v;

            FillFormInfo();
            BindFormContro();
        }

        void FillFormInfo()
        {
            cboCtype.DataSource = EnumUtil.Enum2Table<ClientTypeEnum>();
            cboPreprice.DataSource = EnumUtil.Enum2Table<PrepriceEnum>();
            cboPrediscount.DataSource = EnumUtil.Enum2Table<PrediscountEnum>();
            cboEconomytype.DataSource = _sysdictablebLl.Query((int)SysDicEnum.经济类型);
            cboClientsource.DataSource = _sysdictablebLl.Query((int)SysDicEnum.客户来源);
            cboTrade.DataSource = _sysdictablebLl.Query((int)SysDicEnum.所属行业);
            cboSendtype.DataSource = _sysdictablebLl.Query((int)SysDicEnum.发货方式);
            cboWorktype.DataSource = _sysdictablebLl.Query((int)SysDicEnum.经营类型);
            cboCreditlevel.DataSource = _sysdictablebLl.Query((int)SysDicEnum.客户级别);
            cboClientstate.DataSource = _sysdictablebLl.Query((int)SysDicEnum.客户状态);
        }

        /// <summary>
        /// 绑定控件值
        /// </summary>
        void BindFormContro()
        {
            txtClienttype.Text = _sysdictable.Dicname;
            _clientinfo.Clienttype = _sysdictable.Id;

            if (_clientinfo.Deptid != null)
            {
                var obj = _deptinfoBLL.Get(_clientinfo.Deptid.Value);
                txtDeptid.Text = obj != null ? obj.Deptname : "";
            }

            if (_clientinfo.Salesman != null)
            {
                var obj = _userinfoBLL.Get(_clientinfo.Salesman.Value);
                txtSalesman.Text = obj != null ? obj.Username : "";
            }

            txtClientname.DataBindings.Add(new Binding("Text", _clientinfo, "Clientname", true, DataSourceUpdateMode.OnPropertyChanged));
            cboCtype.DataBindings.Add(new Binding("SelectedValue", _clientinfo, "Ctype", true, DataSourceUpdateMode.OnPropertyChanged));
            txtClienttype.DataBindings.Add(new Binding("Tag", _clientinfo, "Clienttype", true, DataSourceUpdateMode.OnPropertyChanged));
            txtClientpym.DataBindings.Add(new Binding("Text", _clientinfo, "Clientpym", true, DataSourceUpdateMode.OnPropertyChanged));
            txtAddress.DataBindings.Add(new Binding("Text", _clientinfo, "Address", true, DataSourceUpdateMode.OnPropertyChanged));
            txtClientno.DataBindings.Add(new Binding("Text", _clientinfo, "Clientno", true, DataSourceUpdateMode.OnPropertyChanged));
            txtDeptid.DataBindings.Add(new Binding("Tag", _clientinfo, "Deptid", true, DataSourceUpdateMode.OnPropertyChanged));
            txtSalesman.DataBindings.Add(new Binding("Tag", _clientinfo, "Salesman", true, DataSourceUpdateMode.OnPropertyChanged));
            txtLinkman.DataBindings.Add(new Binding("Text", _clientinfo, "Linkman", true, DataSourceUpdateMode.OnPropertyChanged));
            txtTelephone.DataBindings.Add(new Binding("Text", _clientinfo, "Telephone", true, DataSourceUpdateMode.OnPropertyChanged));
            txtMobilephone.DataBindings.Add(new Binding("Text", _clientinfo, "Mobilephone", true, DataSourceUpdateMode.OnPropertyChanged));
            txtFax.DataBindings.Add(new Binding("Text", _clientinfo, "Fax", true, DataSourceUpdateMode.OnPropertyChanged));
            txtRegion.DataBindings.Add(new Binding("Text", _clientinfo, "Region", true, DataSourceUpdateMode.OnPropertyChanged));
            txtPostcode.DataBindings.Add(new Binding("Text", _clientinfo, "Postcode", true, DataSourceUpdateMode.OnPropertyChanged));
            txtEmail.DataBindings.Add(new Binding("Text", _clientinfo, "Email", true, DataSourceUpdateMode.OnPropertyChanged));
            txtExfield1.DataBindings.Add(new Binding("Text", _clientinfo, "Exfield1", true, DataSourceUpdateMode.OnPropertyChanged));
            txtExfield2.DataBindings.Add(new Binding("Text", _clientinfo, "Exfield2", true, DataSourceUpdateMode.OnPropertyChanged));
            txtExfield3.DataBindings.Add(new Binding("Text", _clientinfo, "Exfield3", true, DataSourceUpdateMode.OnPropertyChanged));
            txtExfield4.DataBindings.Add(new Binding("Text", _clientinfo, "Exfield4", true, DataSourceUpdateMode.OnPropertyChanged));
            cboPreprice.DataBindings.Add(new Binding("Text", _clientinfo, "Preprice", true, DataSourceUpdateMode.OnPropertyChanged));
            cboPrediscount.DataBindings.Add(new Binding("Text", _clientinfo, "Prediscount", true, DataSourceUpdateMode.OnPropertyChanged));
            diRecmoney.DataBindings.Add(new Binding("ValueObject", _clientinfo, "Recmoney", true, DataSourceUpdateMode.OnPropertyChanged));
            diPaymoney.DataBindings.Add(new Binding("ValueObject", _clientinfo, "Paymoney", true, DataSourceUpdateMode.OnPropertyChanged));
            diRecmoneysum.DataBindings.Add(new Binding("ValueObject", _clientinfo, "Recmoneysum", true, DataSourceUpdateMode.OnPropertyChanged));
            diPaymoneysum.DataBindings.Add(new Binding("ValueObject", _clientinfo, "Paymoneysum", true, DataSourceUpdateMode.OnPropertyChanged));
            diRecmoneycredit.DataBindings.Add(new Binding("ValueObject", _clientinfo, "Recmoneycredit", true, DataSourceUpdateMode.OnPropertyChanged));
            diPaymoneycredit.DataBindings.Add(new Binding("ValueObject", _clientinfo, "Paymoneycredit", true, DataSourceUpdateMode.OnPropertyChanged));
            diRecday.DataBindings.Add(new Binding("ValueObject", _clientinfo, "Recday", true, DataSourceUpdateMode.OnPropertyChanged));
            diPayday.DataBindings.Add(new Binding("ValueObject", _clientinfo, "Payday", true, DataSourceUpdateMode.OnPropertyChanged));
            txtArtificialperson.DataBindings.Add(new Binding("Text", _clientinfo, "Artificialperson", true, DataSourceUpdateMode.OnPropertyChanged));
            txtRegistermoney.DataBindings.Add(new Binding("Text", _clientinfo, "Registermoney", true, DataSourceUpdateMode.OnPropertyChanged));
            txtBank.DataBindings.Add(new Binding("Text", _clientinfo, "Bank", true, DataSourceUpdateMode.OnPropertyChanged));
            txtAccountno.DataBindings.Add(new Binding("Text", _clientinfo, "Accountno", true, DataSourceUpdateMode.OnPropertyChanged));
            cboEconomytype.DataBindings.Add(new Binding("Text", _clientinfo, "Economytype", true, DataSourceUpdateMode.OnPropertyChanged));
            txtTaxno.DataBindings.Add(new Binding("Text", _clientinfo, "Taxno", true, DataSourceUpdateMode.OnPropertyChanged));
            cboClientsource.DataBindings.Add(new Binding("Text", _clientinfo, "Clientsource", true, DataSourceUpdateMode.OnPropertyChanged));
            cboTrade.DataBindings.Add(new Binding("Text", _clientinfo, "Trade", true, DataSourceUpdateMode.OnPropertyChanged));
            cboSendtype.DataBindings.Add(new Binding("Text", _clientinfo, "Sendtype", true, DataSourceUpdateMode.OnPropertyChanged));
            cboWorktype.DataBindings.Add(new Binding("Text", _clientinfo, "Worktype", true, DataSourceUpdateMode.OnPropertyChanged));
            cboCreditlevel.DataBindings.Add(new Binding("Text", _clientinfo, "Creditlevel", true, DataSourceUpdateMode.OnPropertyChanged));
            cboClientstate.DataBindings.Add(new Binding("Text", _clientinfo, "Clientstate", true, DataSourceUpdateMode.OnPropertyChanged));
            txtBusiarea.DataBindings.Add(new Binding("Text", _clientinfo, "Busiarea", true, DataSourceUpdateMode.OnPropertyChanged));
            txtClientmemo.DataBindings.Add(new Binding("Text", _clientinfo, "Clientmemo", true, DataSourceUpdateMode.OnPropertyChanged));

            chIsvalid.Checked = _clientinfo.Isvalid.Value.Equals(1);
        }

        private void toolStripAdd_Click(object sender, EventArgs e)
        {
            _clientinfo = new Clientinfo();
            ControlUtil.ClearControlBind(superTabControl1);
            BindFormContro();
        }

        private void tsbSave_Click(object sender, EventArgs e)
        {
            SaveInfo();
        }

        private void SaveInfo(bool isAdd = true)
        {
            if (txtClientname.Text.Length == 0)
            {
                MessageBox.Show("姓名不能为空，请输入.", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtClientname.Focus();
                return;
            }

            _clientinfo.Isvalid = chIsvalid.Checked ? (short)1 : (short)0;

            _clientinfoBll.Edit(_clientinfo);

            MessageBox.Show("保存成功.", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            if (isAdd) this.DialogResult = DialogResult.OK;
        }

        private void tsbSaveAdd_Click(object sender, EventArgs e)
        {
            SaveInfo(false);
            _clientinfo = new Clientinfo();
            ControlUtil.ClearControlBind(superTabControl1);
            BindFormContro();
        }

        private void tsmPre_Click(object sender, EventArgs e)
        {
            PreviewRecord(_clientinfo.Id - 1, 0);
        }

        private void tsbNext_Click(object sender, EventArgs e)
        {
            PreviewRecord(_clientinfo.Id + 1, 1);
        }

        /// <summary>
        /// 浏览记录 上一条 下一条
        /// </summary>
        /// <param name="id">浏览记录Id</param>
        /// <param name="type">类型 0-向上,1-向下</param>
        private void PreviewRecord(long id, int type)
        {
            bool isLast = false;
            var obj = _clientinfoBll.PreRecord(id, type, ref isLast);
            _clientinfo = obj;
            ControlUtil.ClearControlBind(superTabControl1);
            BindFormContro();

            if (isLast) MessageBox.Show(string.Format("当前已是{0}一条记录", type.Equals(0) ? "第" : "最后"));
        }

        private void tsbExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtClientname_Leave(object sender, EventArgs e)
        {
            txtClientpym.Text = ChineseToSpell.GetChineseSpell(txtClientname.Text);
        }

        private void btnClienttype_Click(object sender, EventArgs e)
        {
            CreateFormView.BindDicForm(txtClienttype, SysDicEnum.往来单位类别);
            _clientinfo.Clienttype = Normal.ToLong(txtClienttype.Tag);
        }

        private void btnDeptid_Click(object sender, EventArgs e)
        {
            CreateFormView.BindDepartForm(txtDeptid);
            _clientinfo.Deptid = Normal.ToLong(txtDeptid.Tag);
        }

        private void btnSalesman_Click(object sender, EventArgs e)
        {
            CreateFormView.BindStaffForm(txtSalesman);
            _clientinfo.Salesman = Normal.ToLong(txtSalesman.Tag);
        }

        private void btnEconomytype_Click(object sender, EventArgs e)
        {
            CreateFormView.BindDicForm(cboEconomytype, SysDicEnum.经济类型);
        }

        private void btnClientsource_Click(object sender, EventArgs e)
        {
            CreateFormView.BindDicForm(cboClientsource, SysDicEnum.客户来源);
        }

        private void btnTrade_Click(object sender, EventArgs e)
        {
            CreateFormView.BindDicForm(cboTrade, SysDicEnum.所属行业);
        }

        private void btnSendtype_Click(object sender, EventArgs e)
        {
            CreateFormView.BindDicForm(cboSendtype, SysDicEnum.发货方式);
        }

        private void btnWorktype_Click(object sender, EventArgs e)
        {
            CreateFormView.BindDicForm(cboWorktype, SysDicEnum.经营类型);
        }

        private void btnCreditlevel_Click(object sender, EventArgs e)
        {
            CreateFormView.BindDicForm(cboCreditlevel, SysDicEnum.客户级别);
        }

        private void btnClientstate_Click(object sender, EventArgs e)
        {
            CreateFormView.BindDicForm(cboClientstate, SysDicEnum.客户状态);
        }

        private void btnRegion_Click(object sender, EventArgs e)
        {
            CreateFormView.BindDicForm(txtRegion, SysDicEnum.所属地区, true);
        }

    }
}
